package com.example.bank.util;

import cn.hutool.crypto.digest.MD5;
import org.apache.shiro.crypto.hash.Md5Hash;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 * @author ：Leeziqiang
 * @description：TODO
 * @date ：2022/1/17 22:11
 */
public class InterceptorUtil {


    private static String APPID = "13627";

    private static String SECRET = "09cfe421978d3665a9561844037ce112";

    private static String NOTIFY_URL= "https://dingwang.288cloud.com/interface/record";
    // 汽车出险接口
    // 输入车辆大驾号
    public static String accidentRecord(String c_vin) throws IOException {
        BufferedReader in = null;
        String sign0 = "appid"+APPID+"c_vin"+c_vin+"formatjson"+SECRET;
        String sign = new Md5Hash(sign0).toString();
        String url = "https://uaqy.api.storeapi.net/api/106/242?appid="+APPID+"&c_vin="+c_vin+"&format=json"+"&sign="+sign;

        try{
            URL connUrl = new URL(url);
            URLConnection connection = connUrl.openConnection();
            // 设置通用的请求属性
            connection.setConnectTimeout(5000);
            connection.setReadTimeout(5000);
            // 建立实际的连接
            connection.connect();
            // 定义BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            StringBuffer stringBuffer = new StringBuffer();
            String len;
            while ((len = in.readLine()) != null) {
                stringBuffer.append(len);
            }
            return stringBuffer.toString();
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return "error";
        } catch (IOException e) {
            e.printStackTrace();
            return "error";
        } finally {
            if (in != null) {
                in.close();
            }
        }
    }


    // 维修保养记录接口
    public static String maintainRecord(String c_engine, String c_vin) throws IOException {
        BufferedReader in = null;
        String sign0 = "appid"+APPID+"c_engine"+c_engine+"c_vin"+c_vin+"formatjsonnotify_url"+NOTIFY_URL+SECRET;
        String sign = new Md5Hash(sign0).toString();
        String url = "https://uaqy.api.storeapi.net/pyi/170/323?appid="+APPID+"&c_engine="+c_engine+"&c_vin="+c_vin+"&format=json&notify_url="+NOTIFY_URL+"&sign="+sign;
        try{
            URL connUrl = new URL(url);
            URLConnection connection = connUrl.openConnection();
            // 设置通用的请求属性
            connection.setConnectTimeout(5000);
            connection.setReadTimeout(5000);
            // 建立实际的连接
            connection.connect();
            // 定义BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            StringBuffer stringBuffer = new StringBuffer();
            String len;
            while ((len = in.readLine()) != null) {
                stringBuffer.append(len);
            }
            return stringBuffer.toString();
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return "error";
        } catch (IOException e) {
            e.printStackTrace();
            return "error";
        } finally {
            if (in != null) {
                in.close();
            }
        }
    }
}
